/*
 * @Description: model description...
 * @Author: pinus
 * @Email: pinus0716@163.com
 * @Git: https://gitee.com/xyf97
 * @Date: 2021-10-22 19:23:19
 * @LastEditTime: 2021-10-28 15:30:48
 */
// pages/personal/personal.js

import request from '../../utils/request.js'

let touchStartY = 0,
  touchMoveY = 0

Page({
  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: '',
    coverTransition: '',
    userInfo: {
      name: '游客',
      picUrl: '/static/personal/missing-face.png'
    },
    recordList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    // 尝试获取用户信息
    var loginInfo = wx.getStorageSync('loginInfo')
    if (loginInfo) {
      loginInfo = JSON.parse(loginInfo)
      // 获取用户最近1周前8条播放记录
      const record = await request('/user/record', {
        uid: loginInfo.profile.userId,
        type: 1
      })
      if (record.code === 200) {
        let id = 0
        this.setData({
          recordList: record.weekData.splice(0, 8).map(item => {
            item.id = id++
            return item
          })
        })
      }
      this.setData({
        userInfo: {
          name: loginInfo.profile.nickname,
          picUrl: loginInfo.profile.avatarUrl
        }
      })
    }
  },

  // 拖动
  handleTouchStart(e) {
    this.setData({
      coverTransition: ''
    })
    touchStartY = e.touches[0].pageY
    //
  },
  handleTouchMove(e) {
    touchMoveY = e.touches[0].pageY
    let delta = touchMoveY - touchStartY
    if (delta <= 0) {
      delta = 0
    }
    if (delta > 120) {
      delta = 120
    }

    this.setData({
      coverTransform: `translateY(${delta}rpx)`
    })
  },
  handleTouchEnd() {
    this.setData({
      coverTransform: `translateY(0rpx)`,
      coverTransition: `transform 0.5s ease`
    })
  },

  // 去用户信息页，如果未登录则登录
  toUserInfo() {
    const loginInfo = wx.getStorageSync('loginInfo')
    if (!loginInfo) {
      wx.reLaunch({
        url: '/pages/login/login'
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
})
